#include "array.h"

Array::Array( size_t const size ): size( size ), data( new int[size] ), allocator( 2 ), capacity( size ) {
}

Array::Array( size_t const size, int const alloc ): size( size ), data( new int[size] ), allocator( alloc ), capacity( size ) {
}

Array::Array( Array const & a ): size( a.size ), data( new int[a.capacity] ), allocator( a.allocator ) {
	for( int i=0; i < size; ++i ) {
		data[i] = a.data[i];
	}
}
    
Array& Array::operator= ( Array & a ) {
	Array tmp( a );
	swap( tmp );
	return *this;
}


int Array::get( size_t const index ) const {
	if( check( index ) )
		return data[index];
	return 0;
}

void Array::set( size_t const index, int const v ) {
	if( check( index ) )
		data[index] = v;
}

void Array::swap( Array & a ) {
	std::swap( size, a.size );
 	std::swap( data, a.data );
	std::swap( allocator, a.allocator );
	std::swap( capacity, a.capacity );
}

void Array::push_back(int const v) {
	if( size < capacity ) {
		set(size, v);
		size++;
	} 
	else {
		Array tmp( *this );
		capacity *= allocator;
		data =  new int[capacity];
		for(int i=0; i<size; ++i)
        data[i] = tmp.get( i );
		data[size++] = v;
	}        
}


size_t Array::get_size() const {
	return size;
}   


bool Array::check( size_t const index ) const {
	if( index < 0 || index >= size )
		return false;
	return true;
}

Array::~Array() {
    delete[] data;
}

